<template>
  <r-header />
  <div>
    <div class="con">

      <r-logo class="logo" />
      <div>
        <div class="text">手机号</div>
        <input class="input" type="text" v-model="form.account" placeholder="请输入手机号" maxlength="14"
          @blur="validatePhone" />
        <div v-if="phoneError" class="error-text">{{ phoneError }}</div>

        <div class="text">密码</div>
        <input class="input" type="password" v-model="form.password" placeholder="请输入密码" maxlength="14"
          @blur="validatePassword" />
        <div v-if="passwordError" class="error-text">{{ passwordError }}</div>
      </div>
      <div class="button">
        <div class="btlogin" @click="login()" :disabled="isLoading">
          <span v-if="isLoading">登录中...</span>
          <span v-else>立即登录</span>
        </div>
      </div>
      <div class="qwq">
        <div>还没有账号？</div>
        <router-link to="/signup">
          <div class="but">注册账号→</div>
        </router-link>
      </div>

    </div>
  </div>
</template>

<script type="text/javascript">
import axios from 'axios'

export default {
  name: "signin",
  data() {
    return {
      form: {
        account: "",
        password: "",
      },
      phoneError: "",
      passwordError: "",
      isLoading: false
    }
  },
  props: {
  },
  computed: {
  },
  methods: {
    validatePhone() {
      const phoneReg = /^1[3-9]\d{9}$/;
      if (!this.form.account) {
        this.phoneError = "手机号不能为空";
        return false;
      } else if (!phoneReg.test(this.form.account)) {
        this.phoneError = "请输入有效的手机号";
        return false;
      } else {
        this.phoneError = "";
        return true;
      }
    },

    validatePassword() {
      if (!this.form.password) {
        this.passwordError = "密码不能为空";
        return false;
      } else if (this.form.password.length < 6) {
        this.passwordError = "密码长度不能少于6位";
        return false;
      } else {
        this.passwordError = "";
        return true;
      }
    },

    validateForm() {
      const isPhoneValid = this.validatePhone();
      const isPasswordValid = this.validatePassword();
      return isPhoneValid && isPasswordValid;
    },
    login() {
      if (!this.validateForm()) return;

      this.isLoading = true;
      axios.post('/api/auth/login', this.form)
        .then(response => {
          this.isLoading = false;
          if (response.data.success) {
            // 存储token到本地存储
            localStorage.setItem('token', response.data.token);
            // 提交登录状态到 Vuex，切换头部导航
            this.$store.commit('user/login', {
              id: response.data.user.id, // 传递用户ID
              phone_number: response.data.user.phoneNumber,
              account: response.data.user.account,
              name: response.data.user.name,
              avatar: response.data.user.avatar,
              token: response.data.token,
            });
            this.$router.push('/');
            alert(response.data.message || "登录成功");
          } else {
            alert(response.data.message || "登录失败，请重试");
          }
        })
        .catch(error => {
          this.isLoading = false;
          if (error.response) {
            // 服务器返回错误
            alert(error.response.data.message || "登录失败：服务器错误");
          } else if (error.request) {
            // 网络错误
            alert("网络连接失败，请检查网络设置");
          } else {
            // 其他错误
            alert("登录过程出错，请重试");
          }
        });
    }
  },
  mounted() {
  },
}
</script>

<style lang="scss" scoped>
.con {
  display: flex;
  flex-direction: column;
  padding-top: 10px;
  align-items: center;
  justify-content: center;

}

.main {
  // border: 6px solid #090000;
  display: flex;
  flex-direction: column;
  margin-top: 10px;
  background-color: #032850;
  backdrop-filter: blur(3px);
  filter: drop-shadow(5px 5px 10px #000000);
  align-items: center;
  justify-content: center;
}

.logo {
  filter: drop-shadow(15px 10px 5px black);
  margin-top: 20px;
  margin-bottom: 40px;
}

.text {
  color: white;
  margin-bottom: 8px;
}

.input {
  position: relative;
  padding: 2px 2px 2px 2px;
  width: 350px;
  height: 40px;
  margin-bottom: 20px;
  font-size: 20px;
  border: 1.5px solid #fafafa;
  border-radius: 10px;
  background-color: transparent;
  outline: none;
  flex: 1;
  color: white;
  transition: all 0.5s;
}

.input:focus {
  box-shadow: 0 0 5px #03e9f4, 0 0 10px #03e9f4, 0 0 25px #03e9f4,
}

.button {
  display: flex;
  flex-direction: column;
  color: #ffffff;
}

.btlogin {
  background: #0e5271;
  border-radius: 10px;
  margin: 20px 20px;
  padding: 5px;
  width: 200px;
  height: 40px;
  text-align: center;
  padding-top: 10px;
  text-decoration: none;
  transition: all 0.5s;
}

.btlogin:hover {
  background-color: #03e9f4;
  box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4,
}
.qwq {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 14px;
    height: 40px;
    color: #ffffff;
    font-size: 0.875rem;

    .but {
      margin-left: 16px;
      padding: 10px 16px;
      width: 106px;
      font-size: 0.875rem;
      font-weight: 600;
      color: #37bcf7;
      border: 2px solid #37bcf7;
      border-radius: 0.5rem;
      cursor: pointer;

    }
  }


@media screen and (max-width:660px) {
  .con {
    padding-top: 80px;
   }
.input {
  width: 250px;
}
}
</style>